package qq.android.wan.qq;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.provider.Settings;
import android.provider.SyncStateContract;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import java.util.ArrayList;

import qq.android.wan.qq.chat.MyDateBaseHelper;
import qq.android.wan.qq.util.IntentUtil;

/**
 * Created by Administrator on 2015/12/11.
 */
public class MessageFragment extends Fragment {
    private ListView lv_history;
    private ItemLvHistoryAdapter adapter;
    private LinearLayout layout_net;
    private Tab1AddHistoryRecever tab1AddHistoryRecever;
    private NetChangeRecever netChangeRecever;
    private ArrayList<FriendBean> historys;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_tab1,container);
        lv_history=(ListView)view.findViewById(R.id.lv_history);
        layout_net=(LinearLayout)view.findViewById(R.id.layout_net);
        boolean isConnectionNet=new IntentUtil().isConnectionNet(getActivity());
        if (!isConnectionNet){

            layout_net.setVisibility(View.VISIBLE);
            layout_net.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent =  new Intent(Settings.ACTION_SETTINGS);
                    startActivity(intent);
                }
            });
        }
        getChatHistory();
        adapter=new ItemLvHistoryAdapter(getActivity(),historys);
        lv_history.setAdapter(adapter);
        tab1AddHistoryRecever =new Tab1AddHistoryRecever(adapter,historys);
        lv_history.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                FriendBean friendBean=new FriendBean();
                intent.putExtra("friendBean", historys.get(i));
                //intent.putExtra("name", historys.get(i).getNick());
                //intent.putExtra("name", historys.get(i).getNick());
                getActivity().startActivity(intent);
            }
        });

        getActivity().registerReceiver(tab1AddHistoryRecever, new IntentFilter("tab1"));
        netChangeRecever=new NetChangeRecever(layout_net);
        IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(netChangeRecever,intentFilter);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(tab1AddHistoryRecever);
        getActivity().unregisterReceiver(netChangeRecever);
    }


    private void getChatHistory(){
        MyDateBaseHelper dateBaseHelper=new MyDateBaseHelper(getActivity());
        SQLiteDatabase db=dateBaseHelper.getWritableDatabase();
        Cursor cursor=db.rawQuery("select * from chatHistory order by lastTime desc", null);
        historys=new ArrayList<FriendBean>();
        while(cursor.moveToNext()){
            FriendBean bean=new FriendBean();
            bean.setNum(cursor.getString(cursor.getColumnIndex("qqId")));
            bean.setLastMessage(cursor.getString(cursor.getColumnIndex("lastMessage")));
            bean.setNick(cursor.getString(cursor.getColumnIndex("nick")));
            bean.setLastTime(cursor.getString(cursor.getColumnIndex("lastTime")));
            historys.add(bean);
        }
        for(FriendBean f:historys)
        cursor.close();
        db.close();
    }

}
